<?php
/**
 * Created by PhpStorm.
 * User: tangmusen
 * Date: 2017/10/30
 * Time: 11:08
 */
namespace app\home\controller;

use app\admin\model\Members;
use think\Config;
use think\Controller;
use think\Db;
use think\Request;

class Mine extends Base
{
    /**
     * 个人中心
     */
    public function index(){
        $uid = session('uid');
        if($uid) {
            $data = Db::name('member')->field('nickname,mobile,money,head')->where(array('id' => $uid))->find();
            $back_money = Db::name('detail')
                ->where(array('uid' => $uid))
                ->where('exp', 'in', '3,6')
                ->where('status', '2')
                ->sum('money');
            if ($back_money) {
                $back_money = number_format($back_money, 2);
            } else {
                $back_money = '0.00';
            }
            $data['back_money'] = $back_money;
        }elseif(is_weixin()){
            $WxUser = session('wxUser');
            $data['nickname'] = $WxUser['nickname'];
            $data['mobile']   = '未注册';
            $data['money']    = '0.00';
            $data['head']     = $WxUser['headimgurl'];
            $data['back_money'] = '0.00';
        }else{
            $data['nickname']   = '游客'.rand(1111,9999);
            $data['mobile']     = '未登陆';
            $data['money']      = '0.00';
            $data['head']       = '';
            $data['back_money'] = '0.00';
        }
        if($uid){
            $is_login = 1;
        }else{
            $is_login = 0;
        }
        if(is_weixin()){
            $is_weixin=1;
        }else{
            $is_weixin=0;
        }
        return view('',[
            'data'=>$data,
            'is_login'=>$is_login,
            'is_weixin'=>$is_weixin
        ]);
    }

    /**
     * 交易记录
     */
    public function transaction_record(Request $request){
        $b_uid = $request->param('b_uid');
        header("Content-type: text/html; charset=utf-8");
            web_check();
            if($b_uid) {
                $uid = $b_uid;
                $b_name = Db::name('member')->where('id',$b_uid)->value('nickname');
            }else {
                $b_uid = 0;
                $b_name="";
                $uid = session('uid');
            }
            $cate    = $request ->param('cate')?$request ->param('cate'):0;
            $start_p = $request ->param('start')?$request ->param('start'):date('Y-m-d',strtotime('-60 day'));
            $end_P   = $request ->param('end')?$request ->param('end'):date('Y-m-d');
            $page    = $request ->param('page')?$request->param('page'):1;
            $limit = 20;
            $start = ($page-1)*$limit;
            if(!$cate){
                $start_p = strtotime($start_p . ' 00:00:00');
                $end_P   = strtotime($end_P . ' 23:59:59');
                $condition['dl_single.uid'] = $uid;
                $condition['dl_single.create_at'] = array('between', [$start_p, $end_P]);
                $data = Db::name('single')
                    ->alias('s')
                    ->field('s.id,s.cate,s.hall,c.name as name,s.number,s.state,s.code,s.money,s.z_money,s.stage,s.create_at,b.title')
                    ->join('dl_cate c', 's.cate = c.id')
                    ->join('dl_bet b', 's.type = b.type and s.cate =b.cate')
                    ->where($condition)
                    ->limit($start, $limit)
                    ->order('s.id desc')
                    ->select();
                if ($data) {
                    foreach ($data as $k => $v) {
                      $data[$k]['number'] = $v['title'] . '(' . $v['number'] . ')';
                        $data[$k]['name'] =  $v['name'] . '-'.$v['hall'];
                    }
                }
                $return_data['data'] = $data;
                $count = Db::name('single')
                    ->where($condition)
                    ->count();
            }else {
                if ($cate != 14 && $cate != 13) {
                    if ($start_p && $end_P) {
                        $start_p = strtotime($start_p . ' 00:00:00');
                        $end_P = strtotime($end_P . ' 23:59:59');
                        $condition['dl_single.create_at'] = array('between', [$start_p, $end_P]);
                    }
                    $condition['dl_single.uid'] = $uid;
                    $condition['dl_single.cate'] = $cate;
                    $data = Db::name('single')
                        ->alias('s')
                        ->field('s.id,s.cate,s.hall,c.name as name,s.number,s.state,s.code,s.money,s.z_money,s.stage,s.create_at,b.title')
                        ->join('dl_cate c', 's.cate = c.id')
                        ->join('dl_bet b', 'b.cate=s.cate and b.type=s.type')
                        ->where($condition)
                        ->limit($start, $limit)
                        ->order('s.id desc')
                        ->select();
                    if ($data) {
                        foreach ($data as $k => $v) {
                         $data[$k]['number'] = $v['title'] . '(' . $v['number'] . ')';
                            $data[$k]['name'] =  $v['name'] . '-'.$v['hall'];

                        }
                    }
                    $count = Db::name('single')
                        ->where($condition)
                        ->count();
                    $return_data['data'] = $data;
                }
                if ($cate == 13) {
                    $game_name = Db::name('member')->where('id', $uid)->value('gm_name');
                    if ($start_p && $end_P) {
                        $start_p = $start_p . ' 00:00:00';
                        $end_P = $end_P . ' 23:59:59';
                        $condition['transactionTime'] = array('between', [$start_p, $end_P]);
                    }
                    $condition['username'] = $game_name;
                    $data = Db::name('at_ag')
                        ->field('betOrderNo,netPnl,betAmount,transactionTime,gameCode')
                        ->where($condition)
                        ->limit($start, $limit)
                        ->order('betOrderNo desc')
                        ->select();
                    if ($data) {
                        foreach ($data as $k => $v) {
                            $ag_zh = ag_type($v['gameCode']);
                            $daa[$k]['cate']   = 13;
                            $daa[$k]['stage']  = 'AG';
                            $daa[$k]['name']   = $ag_zh['big'];
                            $daa[$k]['number'] = $ag_zh['name'];
                            $daa[$k]['create_at'] = strtotime($v['transactionTime']);
                            if ($v['netPnl'] > 0) {
                                $daa[$k]['state'] = 1;
                                $daa[$k]['code'] = 1;
                                $daa[$k]['money'] = abs($v['betAmount']);
                                $daa[$k]['z_money'] = $v['netPnl'];
                            } elseif ($v['netPnl'] < 0) {
                                $daa[$k]['state'] = 1;
                                $daa[$k]['code'] = 0;
                                $daa[$k]['money'] = abs($v['betAmount']);
                                $daa[$k]['z_money'] = $v['netPnl'];
                            } else {
                                $daa[$k]['state'] = 0;
                                $daa[$k]['code'] = 0;
                                $daa[$k]['money'] = abs($v['betAmount']);
                                $daa[$k]['z_money'] = 0;
                            }
                        }
                    } else {
                        $daa = [];
                    }
                    $count = Db::name('at_ag')
                        ->where($condition)
                        ->count();
                    $return_data['data'] = $daa;
                }
                if ($cate == 14) {

                    $game_name = Db::name('member')->where('id', $uid)->value('gm_name');
                    if ($start_p && $end_P) {
                        $start_p = $start_p . ' 00:00:00';
                        $end_P = $end_P . ' 23:59:59';
                        $condition['WagersDate'] = array('between', [$start_p, $end_P]);
                    }
                    $condition['UserName'] = $game_name;

                    $data = Db::name('at_bb')
                        ->field('WagersID,Payoff,BetAmount,WagersDate,GameType')
                        ->where($condition)
                        ->limit($start, $limit)
                        ->order('WagersID desc')
                        ->select();
                    if ($data) {
                        foreach ($data as $k => $v) {
                            $ag_zh = bb_type($v['GameType']);
                            $daa[$k]['cate'] = 14;
                            $daa[$k]['stage']  = 'BB';
                            $daa[$k]['name']   = $ag_zh['big'];
                            $daa[$k]['number'] = $ag_zh['name'];
                            $daa[$k]['create_at'] = strtotime($v['WagersDate']);
                            if ($v['Payoff'] > 0) {
                                $daa[$k]['state'] = 1;
                                $daa[$k]['code'] = 1;
                                $daa[$k]['z_money'] = $v['Payoff'];
                                $daa[$k]['money'] = abs($v['BetAmount']);;
                            } elseif ($v['Payoff'] < 0) {
                                $daa[$k]['state'] = 1;
                                $daa[$k]['code'] = 0;
                                $daa[$k]['money']  = abs($v['BetAmount']);
                                $daa[$k]['z_money'] = $v['Payoff'];
                            } else {
                                $daa[$k]['state'] = 0;
                                $daa[$k]['code']  = 0;
                                $daa[$k]['money']  = abs($v['BetAmount']);
                                $daa[$k]['z_money'] = 0;
                            }
                        }
                    } else {
                        $daa = [];
                    }
                    $count = Db::name('at_bb')
                        ->where($condition)
                        ->count();
                    $return_data['data'] = $daa;
                }
            }
            if($cate) {
                $cate_name = Db::name('cate')->where('id', $cate)->value('name');
            }else{
                $cate_name = "全部";
            }
            $return_data['now_page'] = 1;
            $return_data['per_page'] = $limit;
            $return_data['total_page'] = ceil($count / $limit);
             return view('',[
                 'data'=>$return_data,
                 'cate'=>$cate,
                 'cate_name'=>$cate_name,
                 'b_uid'=>$b_uid,
                 'b_name'=>$b_name
             ]);
    }

    /**
     * 交易记录
     */
    public function transaction_record_api(Request $request){
        $b_uid = $request->param('b_uid');
        $cate    = $request ->param('cate')?$request ->param('cate'):0;
        $start_p = $request ->param('start')?$request ->param('start'):date('Y-m-d',strtotime('-90 day'));
        $end_P   = $request ->param('end')?$request ->param('end'):date('Y-m-d');
        $page    = $request ->param('page');
        if($b_uid) {
            $uid = $b_uid;
        }else {
            $uid = session('uid');
        }
        $limit = 20;
        $start = ($page-1)*$limit;
        if(!$cate){
            $start_p = strtotime($start_p . ' 00:00:00');
            $end_P   = strtotime($end_P . ' 23:59:59');
            $condition['dl_single.uid'] = $uid;
            $condition['dl_single.create_at'] = array('between', [$start_p, $end_P]);
            $data = Db::name('single')
                ->alias('s')
                ->field('s.id,s.cate,c.name as name,s.number,s.state,s.code,s.money,s.z_money,s.stage,s.create_at,b.title')
                ->join('dl_cate c', 's.cate = c.id')
                ->join('dl_bet b', 's.type = b.type and s.cate =b.cate')
                ->where($condition)
                ->limit($start, $limit)
                ->order('s.id desc')
                ->select();
            if ($data) {
                foreach ($data as $k => $v) {
                    $data[$k]['number'] = $v['title'] . '(' . $v['number'] . ')';
                    $data[$k]['create_at'] = date('m-d H:i',$v['create_at']);
                }
            }
            $return_data['data'] = $data;
            $count = Db::name('single')
                ->where($condition)
                ->count();
        }else {
            if ($cate != 14 && $cate != 13) {
                if ($start_p && $end_P) {
                    $start_p = strtotime($start_p . ' 00:00:00');
                    $end_P = strtotime($end_P . ' 23:59:59');
                    $condition['dl_single.create_at'] = array('between', [$start_p, $end_P]);
                }
                $condition['dl_single.uid'] = $uid;
                $condition['dl_single.cate'] = $cate;
                $data = Db::name('single')
                    ->alias('s')
                    ->field('s.id,s.cate,c.name as name,s.number,s.state,s.code,s.money,s.z_money,s.stage,s.create_at,b.title')
                    ->join('dl_cate c', 's.cate = c.id')
                    ->join('dl_bet b', 'b.cate=s.cate and b.type=s.type')
                    ->where($condition)
                    ->limit($start, $limit)
                    ->order('s.id desc')
                    ->select();
                if ($data) {
                    foreach ($data as $k => $v) {
                     $data[$k]['number'] = $v['title'] . '(' . $v['number'] . ')';
                     $data[$k]['create_at'] = date('m-d H:i',$v['create_at']);
                    }
                }
                $count = Db::name('single')
                    ->where($condition)
                    ->count();
                $return_data['data'] = $data;
            }
            if ($cate == 13) {
                $game_name = Db::name('member')->where('id', $uid)->value('gm_name');
                if ($start_p && $end_P) {
                    $start_p = $start_p . ' 00:00:00';
                    $end_P = $end_P . ' 23:59:59';
                    $condition['transactionTime'] = array('between', [$start_p, $end_P]);
                }
                $condition['username'] = $game_name;
                $data = Db::name('at_ag')
                    ->field('betOrderNo,netPnl,betAmount,transactionTime,gameCode')
                    ->where($condition)
                    ->limit($start, $limit)
                    ->order('betOrderNo desc')
                    ->select();
                if ($data) {
                    foreach ($data as $k => $v) {
                        $ag_zh = ag_type($v['gameCode']);
                        $daa[$k]['cate']   = 13;
                        $daa[$k]['stage']  = 'AG';
                        $daa[$k]['name']   = $ag_zh['big'];
                        $daa[$k]['number'] = $ag_zh['name'];
                        $daa[$k]['create_at'] = date('m-d H:i',strtotime($v['transactionTime']));
                        if ($v['netPnl'] > 0) {
                            $daa[$k]['state'] = 1;
                            $dat[$k]['code'] = 1;
                            $daa[$k]['money'] = abs($v['betAmount']);
                            $daa[$k]['z_money'] = $v['netPnl'];
                        } elseif ($v['netPnl'] < 0) {
                            $daa[$k]['state'] = 1;
                            $daa[$k]['code'] = 0;
                            $daa[$k]['money'] = abs($v['betAmount']);
                            $daa[$k]['z_money'] = $v['netPnl'];
                        } else {
                            $daa[$k]['state'] = 0;
                            $daa[$k]['code'] = 0;
                            $daa[$k]['money'] = abs($v['betAmount']);
                            $daa[$k]['z_money'] = 0;
                        }
                    }
                } else {
                    $daa = [];
                }
                $count = Db::name('at_ag')
                    ->where($condition)
                    ->count();
                $return_data['data'] = $daa;
            }
            if ($cate == 14) {
                $game_name = Db::name('member')->where('id', $uid)->value('gm_name');
                if ($start_p && $end_P) {
                    $start_p = $start_p . ' 00:00:00';
                    $end_P = $end_P . ' 23:59:59';
                    $condition['WagersDate'] = array('between', [$start_p, $end_P]);
                }
                $condition['UserName'] = $game_name;

                $data = Db::name('at_bb')
                    ->field('WagersID,Payoff,BetAmount,WagersDate,GameType')
                    ->where($condition)
                    ->limit($start, $limit)
                    ->order('WagersID desc')
                    ->select();
                if ($data) {
                    foreach ($data as $k => $v) {
                        $ag_zh = bb_type($v['GameType']);
                        $daa[$k]['cate'] = 14;
                        $daa[$k]['stage']  = 'BB';
                        $daa[$k]['name']   = $ag_zh['big'];
                        $daa[$k]['number'] = $ag_zh['name'];
                        $daa[$k]['create_at'] = date('m-d H:i',strtotime($v['WagersDate']));
                        if ($v['Payoff'] > 0) {
                            $daa[$k]['state'] = 1;
                            $daa[$k]['code'] = 1;
                            $daa[$k]['z_money'] = $v['Payoff'];
                            $daa[$k]['money'] = abs($v['BetAmount']);;
                        } elseif ($v['Payoff'] < 0) {
                            $daa[$k]['state'] = 1;
                            $daa[$k]['code'] = 0;
                            $daa[$k]['money']  = abs($v['BetAmount']);
                            $daa[$k]['z_money'] = $v['Payoff'];
                        } else {
                            $daa[$k]['state'] = 0;
                            $daa[$k]['code']  = 0;
                            $daa[$k]['money']  = abs($v['BetAmount']);
                            $daa[$k]['z_money'] = 0;
                        }
                    }
                } else {
                    $daa = [];
                }
                $count = Db::name('at_bb')
                    ->where($condition)
                    ->count();
                $return_data['data'] = $daa;
            }
        }
        $return_data['cate'] = $cate;
        $return_data['now_page'] = $page;
        $return_data['per_page'] = $limit;
        $return_data['total_page'] = ceil($count / $limit);
      json_return(200,'成功',$return_data);
    }

    /**
     * 代理交易记录
     */
    public function proxy_transaction_record(Request $request){

        header("Content-type: text/html; charset=utf-8");
        web_check();
        $uid =session('uid');
        $uids      = $this->get_uids($uid)['uids'];
        $uids[]    = $uid;
        $game_name = $this->get_uids($uid)['gm_name'];
        $cate    = $request ->param('cate')?$request ->param('cate'):1;
        $hall    = $request ->param('hall')?$request ->param('hall'):1;
        $start_p = $request ->param('start')?$request ->param('start'):date('Y-m-d',strtotime('-180 day'));
        $end_P   = $request ->param('end')?$request ->param('end'):date('Y-m-d');
        $page    = $request ->param('page')?$request->param('page'):1;
        $limit = 20;
        $start = ($page-1)*$limit;
        if($cate!=13 && $cate!=14){
            $start_p = strtotime($start_p . ' 00:00:00');
            $end_P   = strtotime($end_P . ' 23:59:59');
            $condition['dl_single.uid']  =['in',$uids];
            $condition['dl_single.hall'] = $hall;
            $condition['dl_single.cate'] = $cate;
            $condition['dl_single.create_at'] = array('between', [$start_p, $end_P]);
            $data = Db::name('single')
                ->alias('s')
                ->field('s.id,s.cate,s.hall,c.name as name,s.number,s.state,s.code,s.money,s.z_money,s.stage,s.create_at,b.title,m.nickname')
                ->join('dl_cate c', 's.cate = c.id')
                ->join('dl_bet b', 's.type = b.type and s.cate =b.cate')
                ->join('dl_member m', 's.uid = m.id')
                ->where($condition)
                ->limit($start, $limit)
                ->order('s.id desc')
                ->select();
            if ($data) {
                foreach ($data as $k => $v) {
                    $data[$k]['number'] = $v['title'] . '(' . $v['number'] . ')';
                    $data[$k]['name'] =  $v['name'] . '-'.$v['hall'];
                }
            }
            $return_data['data'] = $data;
            $count = Db::name('single')
                ->where($condition)
                ->count();
        }
        if ($cate == 13) {
            if ($start_p && $end_P) {
                $start_p = $start_p . ' 00:00:00';
                $end_P = $end_P . ' 23:59:59';
                $condition['transactionTime'] = array('between', [$start_p, $end_P]);
            }
            $condition['username'] = ['in',$game_name];
            $condition['hall']     = $hall;
            $data = Db::name('at_ag')
                ->alias('a')
                ->field('betOrderNo,netPnl,betAmount,transactionTime,gameCode,m.nickname')
                ->join('dl_member m','a.username=m.gm_name')
                ->where($condition)
                ->limit($start, $limit)
                ->order('betOrderNo desc')
                ->select();
            if ($data) {
                foreach ($data as $k => $v) {
                    $ag_zh = ag_type($v['gameCode']);
                    $daa[$k]['cate']   = 13;
                    $daa[$k]['stage']  = 'AG';
                    $daa[$k]['name']   = $ag_zh['big'];
                    $daa[$k]['nickname']  = $v['nickname'];
                    $daa[$k]['number'] = $ag_zh['name'];
                    $daa[$k]['create_at'] = strtotime($v['transactionTime']);
                    if ($v['netPnl'] > 0) {
                        $daa[$k]['state'] = 1;
                        $daa[$k]['code'] = 1;
                        $daa[$k]['money'] = abs($v['betAmount']);
                        $daa[$k]['z_money'] = $v['netPnl'];
                    } elseif ($v['netPnl'] < 0) {
                        $daa[$k]['state'] = 1;
                        $daa[$k]['code'] = 0;
                        $daa[$k]['money'] = abs($v['betAmount']);
                        $daa[$k]['z_money'] = $v['netPnl'];
                    } else {
                        $daa[$k]['state'] = 0;
                        $daa[$k]['code'] = 0;
                        $daa[$k]['money'] = abs($v['betAmount']);
                        $daa[$k]['z_money'] = 0;
                    }
                }
            } else {
                $daa = [];
            }
            $count = Db::name('at_ag')
                ->where($condition)
                ->count();
            $return_data['data'] = $daa;
        }
        if ($cate == 14) {
            $game_name = Db::name('member')->where('id', $uid)->value('gm_name');
            if ($start_p && $end_P) {
                $start_p = $start_p . ' 00:00:00';
                $end_P = $end_P . ' 23:59:59';
                $condition['WagersDate'] = array('between', [$start_p, $end_P]);
            }
            $condition['UserName'] = ['in',$game_name];
            $condition['hall']     = $hall;
            $data = Db::name('at_bb')
                ->alias('a')
                ->field('WagersID,Payoff,BetAmount,WagersDate,GameType,m.nickname')
                ->join('dl_member m','a.username=m.gm_name')
                ->where($condition)
                ->limit($start, $limit)
                ->order('WagersID desc')
                ->select();
            if ($data) {
                foreach ($data as $k => $v) {
                    $ag_zh = bb_type($v['GameType']);
                    $daa[$k]['cate'] = 14;
                    $daa[$k]['stage']  = 'BB';
                    $daa[$k]['name']   = $ag_zh['big'];
                    $daa[$k]['number'] = $ag_zh['name'];
                    $daa[$k]['nickname']  = $v['nickname'];
                    $daa[$k]['create_at'] = strtotime($v['WagersDate']);
                    if ($v['Payoff'] > 0) {
                        $daa[$k]['state'] = 1;
                        $daa[$k]['code'] = 1;
                        $daa[$k]['z_money'] = $v['Payoff'];
                        $daa[$k]['money'] = abs($v['BetAmount']);;
                    } elseif ($v['Payoff'] < 0) {
                        $daa[$k]['state'] = 1;
                        $daa[$k]['code'] = 0;
                        $daa[$k]['money']  = abs($v['BetAmount']);
                        $daa[$k]['z_money'] = $v['Payoff'];
                    } else {
                        $daa[$k]['state'] = 0;
                        $daa[$k]['code']  = 0;
                        $daa[$k]['money']  = abs($v['BetAmount']);
                        $daa[$k]['z_money'] = 0;
                    }
                }
            } else {
                $daa = [];
            }
            $count = Db::name('at_bb')
                ->where($condition)
                ->count();
            $return_data['data'] = $daa;
        }
        $map =[
            'cate'=>$cate,
            'hall'=>$hall
        ];
        $hall_name = Db::name('hall')->where($map)->value('name');
        $return_data['now_page'] = 1;
        $return_data['per_page'] = $limit;
        $return_data['total_page'] = ceil($count / $limit);
        return view('',[
            'data'  =>$return_data,
            'hall_name'=>$hall_name,
            'cate'=>$cate,
            'hall'=>$hall
        ]);
    }

    /**
     * 代理交易记录
     */
    public function proxy_transaction_record_api(Request $request){
        $uid =session('uid');
        $hall    = $request ->param('hall')?$request ->param('hall'):1;
        $cate    = $request ->param('cate')?$request ->param('cate'):1;
        $start_p = $request ->param('start')?$request ->param('start'):date('Y-m-d',strtotime('-90 day'));
        $end_P   = $request ->param('end')?$request ->param('end'):date('Y-m-d');
        $page    = $request ->param('page');

        $limit = 20;
        $start = ($page-1)*$limit;
        if($cate){
            $start_p = strtotime($start_p . ' 00:00:00');
            $end_P   = strtotime($end_P . ' 23:59:59');
            $condition['dl_single.uid'] = $uid;
            $condition['dl_single.hall'] = $hall;
            $condition['dl_single.cate'] = $cate;
            $condition['dl_single.create_at'] = array('between', [$start_p, $end_P]);
            $data = Db::name('single')
                ->alias('s')
                ->field('s.id,s.cate,c.name as name,s.number,s.state,s.code,s.money,s.z_money,s.stage,s.create_at,b.title,m.nickname')
                ->join('dl_cate c', 's.cate = c.id')
                ->join('dl_bet b', 's.type = b.type and s.cate =b.cate')
                ->join('dl_member m', 's.uid = m.id')
                ->where($condition)
                ->limit($start, $limit)
                ->order('s.id desc')
                ->select();
            if ($data) {
                foreach ($data as $k => $v) {
                    $data[$k]['number'] = $v['title'] . '(' . $v['number'] . ')';
                    $data[$k]['create_at'] = date('m-d H:i',$v['create_at']);
                }
            }
            $return_data['data'] = $data;
            $count = Db::name('single')
                ->where($condition)
                ->count();
        }
        $return_data['cate'] = $cate;
        $return_data['now_page'] = $page;
        $return_data['per_page'] = $limit;
        $return_data['total_page'] = ceil($count / $limit);
        json_return(200,'成功',$return_data);
    }

    /**
     * 统计记录
     */
    public function statistics_record(Request $request){
        web_check();
        $uid     = session('uid');
        $start_p = $request ->param('start');
        $end_p   = $request ->param('end');
        $condition['dl_single.uid'] = $uid;
        if($start_p && $end_p) {
            $start_p_up = strtotime($start_p . ' 00:00:00');
            $end_p_up   = strtotime($end_p . ' 23:59:59');
            $condition['create_at'] = array('between', [$start_p_up, $end_p_up]);
        }
        $data = Db::name('single')
            ->field('cate,sum(money) as betting_money,sum(z_money-money) as win_money')
            ->where($condition)
            ->group('cate')
            ->select();
        $arr = [];
        foreach ($data as $k=>$v){
            switch ($v['cate']){
                case 1:
                    $arr['eight']  = $data[$k];
                    break;
                case 2:
                    $arr['canada'] = $data[$k];
                    break;
                case 3:
                    $arr['car']    = $data[$k];
                    break;
                case 4:
                    $arr['airship'] = $data[$k];
                    break;
                case 5:
                    $arr['ssc'] = $data[$k];
                    break;
                case 6:
                    $arr['tjssc'] = $data[$k];
                    break;
                case 7:
                    $arr['gd10']  = $data[$k];
                    break;
                case 8:
                    $arr['cq10'] = $data[$k];
                    break;
                case 9:
                    $arr['fast'] = $data[$k];
                    break;
                case 10:
                    $arr['gd11'] = $data[$k];
                    break;
                case 11:
                    $arr['hk'] = $data[$k];
                    break;
            }

        }
        $all_data =  Db::name('single')
            ->field('sum(money) as betting_money,sum(z_money-money) as win_money')
            ->where($condition)
            ->find();
        $game_name = Db::name('member')->where('id',$uid)->value('gm_name');
        if($start_p && $end_p) {
            $start_p = $start_p . ' 00:00:00';
            $end_P = $end_p . ' 23:59:59';
            $condition1['transactionTime'] = array('between', [$start_p, $end_P]);
            $condition2['WagersDate'] = array('between', [$start_p, $end_P]);

        }
        $condition1['username'] = $game_name;
        $condition2['UserName'] = $game_name;
        $all_data0 =  Db::name('at_ag')
            ->field('sum(betAmount) as betting_money,sum(netPnl) as win_money')
            ->where($condition1)
            ->find();
        $arr['ag']['betting_money']=$all_data0['betting_money'];
        $arr['ag']['win_money']    =$all_data0['win_money'];

        $all_data1 =  Db::name('at_bb')
            ->field('sum(BetAmount) as betting_money,sum(Payoff) as win_money')
            ->where($condition2)
            ->find();
        $arr['bb']['betting_money']=$all_data1['betting_money'];
        $arr['bb']['win_money']    =$all_data1['win_money'];

        $all_data['betting_money'] = $all_data['betting_money']+$all_data0['betting_money']+$all_data1['betting_money'];
        $all_data['win_money'] = $all_data['win_money']+$all_data0['win_money']+$all_data1['win_money'];
        $total = Db::name('single')
            ->where($condition)
            ->count();
        $total0 = Db::name('at_ag')
            ->where($condition1)
            ->count();
        $total1 = Db::name('at_bb')
            ->where($condition2)
            ->count();
        $total = $total + $total0 + $total1;
        return view('',[
            'data'=>$arr,
            'all_data'=>$all_data,
            'total'=>$total
        ]);
    }

    /**
     * 充值记录
     */
    public function recharge_record(Request $request){
        web_check();
        $uid     = session('uid');
        $page    = $request ->post('page')?$request->post('page'):1;
        $limit = 50;
        $start = ($page-1)*$limit;
        $data = Db::name('recharge')
            ->where('uid',$uid)
            ->limit($start,$limit)
            ->order('id desc')
            ->select();
        return view('',[
            'data'=>$data
        ]);
    }

    /**
     * 我的推广
     */
    public function promotion(){

        return view();
    }

    /**
     * 联系客服
     */
    public function service(){
        web_check();
        $uid = session('uid');
        $data = Db::name('member')->field('nickname,head,mobile')->where('id',$uid)->find();
        $date = date('Y-m-d H:i:s');
        $data['date'] = $date;
        return view('service',$data);
    }

    /**
     * 设置
     */
    public function setting(){
        web_check();
        return view();
    }

    /**
     * 关于我们
     */
    public function about(){

        return view();
    }

    /**
     * 版本信息
     */
    public function version(){

        return view();
    }

    /**
     * 安全中心
     */
    public function account_safe(){

        return view();
    }

    /**
     * 推广中心
     */
    public function promotion_center(){
        web_check();
        $id = session('uid');
        $parent_id = 'yicai'.$id;
        $parent_id = base64_encode(base64_encode(base64_encode($parent_id)));
        $url = Config::get('host_url').'home/index/index?parent_id='.$parent_id;
        return view('',[
            'code_url'=>$url,
        ]);
    }

    /**
     * 推广佣金
     */
    public function my_promotion(Request $request){
        web_check();
        header("Content-type: text/html; charset=utf-8");
        $uid      = session('uid');
        $page    = $request ->post('page')?$request->post('page'):1;
        $is_proxy = Db::name('member')
            ->where('id',$uid)
            ->value('is_proxy');
        if($is_proxy){
            $message = '您是代理，没有推广佣金';
            echo '<script>alert(\''.$message.'\');history.go(-1)</script>';exit;

        }
        $limit = 20;
        $start = ($page-1)*$limit;
        $data = Db::name('member')
            ->field('id,nickname,create_at,mobile,head')
            ->where(array('tid'=>$uid))
            ->limit($start,$limit)
            ->select();
        foreach ($data as $k=>$v){
            $proxy_money =  Db::name('detail')
                ->where(array('proxy_uid'=>$v['id'],'exp'=>4))
                ->sum('money');
            if(!$proxy_money ){
                $proxy_money = '0.00';
            }
            $data[$k]['proxy_money'] = $proxy_money;
            $data[$k]['mobile']      = $str = substr_replace($v['mobile'],'****',3,4);;
        }
        $count=Db::name('member')
            ->where(array('tid'=>$uid))
            ->count();
        $return_data['data'] = $data;
        $return_data['now_page'] = $page;
        $return_data['per_page'] = $limit;
        $return_data['total_page'] = ceil($count/$limit);
        return view('',[
            'count'=>$count,
            'return_data'=>$return_data
        ]);
    }

    /**
     * 推广人数
     */
    public function my_promotions(){
        header("Content-type: text/html; charset=utf-8");
        web_check();
        $uid      = session('uid');
        $is_proxy = Db::name('member')
            ->where('id',$uid)
            ->value('is_proxy');
        if(!$is_proxy){
            $message = '您还没有成为代理，赶紧申请成为代理吧!';
            echo '<script>alert(\''.$message.'\');history.go(-1)</script>';exit;
        }
        $uids = $this->get_uids($uid)['uids'];
        $data = Db::name('member')
            ->field('id,nickname,create_at,mobile,head,level')
            ->where('id','in',$uids)
            ->order('create_at desc')
            ->select();
        foreach ($data as $k=>$v){
            $data[$k]['mobile']      = $str = substr_replace($v['mobile'],'****',3,4);
        }
        $count = count($data);
        return view('',[
           'data'=>$data,
           'count'=>$count
        ]);
    }

    /**
     * 获取代理人员
     */
    public function get_uids($uid){
        $w['tid'] = $uid;
        $proxy_level = Db::name('member')->where('id',$uid)->value('proxy_level');
        $field = 'id,gm_name';
        $list1 = Db::name('member')->field($field)->where($w)->order('id desc')->select();
        $level1 = [];
        foreach ($list1 as $k => $v) {
            $level1[] = $v['id'];
            $daa    [] = $v;
        }
        if ($level1 && $proxy_level>=2) {
            $list2 = Db::name('member')->field($field)->where('tid', 'in', $level1)->order('id desc')->select();
            $level2 = [];
            foreach ($list2 as $kk => $vv) {
                $level2[] = $vv['id'];
                $daa[] = $vv;
            }
            if ($level2 && $proxy_level>=3) {
                $list3 = Db::name('member')->field($field)->where('tid', 'in', $level2)->order('id desc')->select();
                $level3 = [];
                foreach ($list3 as $kkk => $vvv) {
                    $level3[] = $vvv['id'];
                    $daa[] = $vvv;
                }
                if ($level3 && $proxy_level>=4) {
                    $list4 = Db::name('member')->field($field)->where('tid', 'in', $level3)->order('id desc')->select();
                    $level4 = [];
                    foreach ($list4 as $kkkk => $vvvv) {
                        $level4[] = $vvvv['id'];
                        $daa[] = $vvvv;
                    }
                    if ($level4 & $proxy_level>=5) {
                        $list5 = Db::name('member')->field($field)->where('tid', 'in', $level4)->order('id desc')->select();
                        foreach ($list5 as $kkkkk => $vvvvv) {
                            $daa[] = $vvvvv;
                        }
                    }
                }
            }
        }
        foreach($daa as $k=>$v){
            $uids[]   = $v['id'];
            $gm_name[] = $v['gm_name'];
        }
        $back['uids']    = $uids;
        $back['gm_name'] = $gm_name;
        return $back;
    }

    /**
     * 获取代理人员
     */
    public function get_uids_yj($uid){
        $w['tid'] = $uid;
        $proxy_level = Db::name('member')->where('id',$uid)->value('proxy_level');
        $proxy_level = $proxy_level-1;
        $field = 'id,gm_name';
        $list1 = Db::name('member')->field($field)->where($w)->order('id desc')->select();
        $level1 = [];
        foreach ($list1 as $k => $v) {
            $level1[] = $v['id'];
            $daa    [] = $v;
        }
        if ($level1 && $proxy_level>=2) {
            $list2 = Db::name('member')->field($field)->where('tid', 'in', $level1)->order('id desc')->select();
            $level2 = [];
            foreach ($list2 as $kk => $vv) {
                $level2[] = $vv['id'];
                $daa[] = $vv;
            }
            if ($level2 && $proxy_level>=3) {
                $list3 = Db::name('member')->field($field)->where('tid', 'in', $level2)->order('id desc')->select();
                $level3 = [];
                foreach ($list3 as $kkk => $vvv) {
                    $level3[] = $vvv['id'];
                    $daa[] = $vvv;
                }
                if ($level3 && $proxy_level>=4) {
                    $list4 = Db::name('member')->field($field)->where('tid', 'in', $level3)->order('id desc')->select();
                    $level4 = [];
                    foreach ($list4 as $kkkk => $vvvv) {
                        $level4[] = $vvvv['id'];
                        $daa[] = $vvvv;
                    }
                    if ($level4 & $proxy_level>=5) {
                        $list5 = Db::name('member')->field($field)->where('tid', 'in', $level4)->order('id desc')->select();
                        foreach ($list5 as $kkkkk => $vvvvv) {
                            $daa[] = $vvvvv;
                        }
                    }
                }
            }
        }
        foreach($daa as $k=>$v){
            $uids[]   = $v['id'];
            $gm_name[] = $v['gm_name'];
        }
        $back['uids'] = $uids;
        $back['gm_name'] = $gm_name;
        return $back;
    }

    /**
     * 我的代理
     */
    public function agent(Request $request){
        web_check();
        header("Content-type: text/html; charset=utf-8");
        $start_p = $request ->param('start');
        $end_p   = $request ->param('end');
        $uid     = session('uid');
        if($start_p && $end_p){
            $start_p_other = strtotime($start_p);
            $end_p_other   = strtotime($end_p);
            $w['create_at']           = ['between',[$start_p_other,$end_p_other] ];
            $wag['transactionTime']   = ['between', [$start_p,$end_p]];
            $wbb['WagersDate']        = ['between', [$start_p,$end_p]];
        }
        if(!$uid ){
            json_return(204,'缺少参数');
        }
        $is_proxy = Db::name('member')
            ->where('id',$uid)
            ->value('is_proxy');
        if(!$is_proxy){
            $message = '您还没有成为代理，赶紧申请成为代理吧!';
            echo '<script>alert(\''.$message.'\');history.go(-1)</script>';exit;
        }
        $back       = $this->get_uids($uid);
        $uids       = $back['uids'];
        $game_names = $back['gm_name'];
        $uids_yj    = $this->get_uids_yj($uid)['uids'];
        $halls = Db::name('hall')->select();
        $total_money = '0.00';
        $all_my_win  = '0.00';
        foreach ($halls as $k=>$v){
            $w['uid'] = ['in', $uids];
            $w['cate'] = $v['cate'];
            $w['hall'] = $v['hall'];
            if($v['cate']!=13 && $v['cate']!=14) {
                $data = Db::name('single')
                    ->where($w)
                    ->sum('z_money-money');
                if($data){
                    $total_money = $data;
                }else{
                    $total_money = '0.00';
                }
            }
            if($v['cate']==13) {
                $wag['username']          = ['in', $game_names];
                $wag['hall']              = $v['hall'];
                $data = Db::name('at_ag')
                    ->where($wag)
                    ->sum('netPnl');
                if($data){
                    $total_money = $data ;
                }else{
                    $total_money = '0.00';
                }
            }
            if($v['cate']==14) {
                $wbb['UserName']   = ['in', $game_names];
                $wbb['hall']       = $v['hall'];
                $data = Db::name('at_bb')
                    ->where($wbb)
                    ->sum('Payoff');
                if($data){
                    $total_money = $data ;
                }else{
                    $total_money = '0.00';
                }
            }
            $back_money = Db::name('detail')
                ->where($w)
                ->where('exp','in','3,6')
                ->where('status',2)
                ->sum('money');
            $w0['uid']  = ['in', $uids_yj];
            $w0['cate'] = $v['cate'];
            $w0['hall'] = $v['hall'];
            $back_yj = Db::name('detail')
                ->where($w0)
                ->where('exp',4)
                ->where('status',2)
                ->sum('money');
            $back_money = $back_money+$back_yj;
            if(!$back_money){
                $back_money = 0.00;
            }
            $ww['cate'] = $v['cate'];
            $ww['hall'] = $v['hall'];
            $fact_money = $total_money+$back_money;
            $back_data = Db::name('water')->where($ww)->find();
            $percent    = $back_data['dl_water'];
            $my_win     = $fact_money * $percent;
            $my_win     = round($my_win,2);
            if($my_win>0){
                $my_win = -$my_win;
            }elseif ($my_win<=0){
                $my_win = abs($my_win);
            }
            $all_back_data[$k]['cate']        = $v['cate'];
            $all_back_data[$k]['hall']        = $v['hall'];
            $all_back_data[$k]['total_money'] = number_format($total_money,2);
            $all_back_data[$k]['back_money']  = number_format($back_money,2);
            $all_back_data[$k]['fact_money']  = number_format($fact_money,2);
            $all_back_data[$k]['my_win']      = number_format($my_win,2);
            $all_back_data[$k]['room']        = $v['name'];
            $all_my_win = $all_my_win + $my_win;
        }
        $return['all_my_win'] = round($all_my_win,2);
        $return['data']       = $all_back_data;
        return view('',$return);
    }

    /**
     * 提现
     */
    public function withdraw(){
        web_check();
        return view();
    }

    /**
     * 支付宝微信提现
     */
    public function withdraw_list(Request $request){
        $uid  = session('uid');
        $way = $request->param('way');
        $data =Db::name('member')
               ->field('true_name,wx,alipay,wx_qrcode,alipay_qrcode')
               ->where('id',$uid)
               ->find();
        $daa['true_name']  = $data['true_name'];
        if($way==1){
            if($data['wx_qrcode']) {
                $daa['qrcode'] = Config::get('img_url').$data['wx_qrcode'];
                $daa['qrcode_s'] = $data['wx_qrcode'];
            }else{
                $daa['qrcode'] = "";
                $daa['qrcode_s'] = "";
            }
            $daa['account'] = $data['wx'] ;
        }
        if($way==2){
            if($data['alipay_qrcode']) {
                $daa['qrcode'] = Config::get('img_url').$data['alipay_qrcode'];
                $daa['qrcode_s'] = $data['alipay_qrcode'];
            }else{
                $daa['qrcode']   = "";
                $daa['qrcode_s'] = "";
            }
            $daa['account'] = $data['alipay'] ;
        }
         $daa['way'] = $way;
        return view('',[
            'data'=>$daa

        ]);
    }

    /**
     * 银行卡提现
     */
    public function withdraw_card(){

        $uid  = session('uid');
        $data =Db::name('member')
            ->field('true_name,bank,branch_bank,bank_card')
            ->where('id',$uid)
            ->find();
        $data['way'] = 3;
        $bank = Db::name('banks')->select();
        return view('',[
            'data'=>$data,
            'bank'=>$bank
        ]);
    }

    /**
     * 用户提现
     */
    public function do_withdrawals(Request $request){

        $uid           = session('uid');
        $username      = $request->post('username');
        $bank_name     = $request->post('bank_name');
        $account_bank  = $request->post('account_bank');
        $branch_name   = $request->post('branch_name');
        $money         = $request->post('money');
        $way           = $request->post('way');
        $qrcode        = $request->post('qrcode');
        if(!$username || !$money ||!$way){
            json_return(201,'网络错误!');
        }
        $user = Members::get($uid);
        $my_money = $user['money'];
        $m_status = $user['m_status'];
        if($m_status==1){
            json_return(201,'您的账号已被冻结!');
        }
        if($my_money<$money){
            json_return(207,'余额不足');
        }
        if($way==1) {
            $up['wx'] = $account_bank;;
            $up['wx_qrcode'] = $qrcode;
            Db::name('member')->where(array('id' => $uid))->update($up);
            $branch_name = '微信';
            $bank_name   = '微信';
        }
        if($way==2) {
            $up['alipay'] = $account_bank;
            $up['alipay_qrcode'] = $qrcode;
            Db::name('member')->where(array('id' => $uid))->update($up);
            $branch_name = '支付宝';
            $bank_name   = '支付宝';
        }
        if($way==3) {
            $up['true_name'] = $username;
            $up['branch_bank'] = $branch_name;
            $up['bank_card'] = $account_bank;
            $up['bank'] = $bank_name;
            Db::name('member')->where(array('id' => $uid))->update($up);
        }
        if(!$qrcode){
            $qrcode = "";
        }
        Db::startTrans();
        try {
            $new_money = bcsub(round($my_money,2), round($money,2));
            $result0 = Db::name('member')
                ->where(array('id'=>$uid))
                ->update(array('money'=>$new_money));
            $data['uid']          = $uid;
            $data['username']     = $username;
            $data['bank_name']    = $bank_name;
            $data['account_bank'] = $account_bank;
            $data['branch_name']  = $branch_name;
            $data['money']        = $money;
            $data['qrcode']       = $qrcode;
            $data['way']          = $way;
            $data['create_at']    = time();
            $result = Db::name('withdrawals')->insert($data);
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            json_return(500,'提现失败');
        }
        if($result && $result0){
            json_return(200,'申请提现成功,后台将在最短时间内审核');
        }else{
            json_return(500,'提现失败');
        }

    }

    /**
     * 提现记录
     */
    public function withdraw_record(){
        $uid = session('uid');

        $data = Db::name('withdrawals')
            ->field('id,money,create_at,way,status')
            ->where('uid',$uid)
            ->order('id desc')
            ->select();

        return view('',[
            'data'=>$data
        ]);
    }

    /**
     * 个人信息
     */
    public function personal(){
        web_check();
        $uid = session('uid');
        $data = Db::name('member')
            ->where('id',$uid)
            ->field('id,head,nickname,mobile,qq,wx,alipay,true_name,bank,branch_bank,bank_card')
            ->find();
        return view('',[
            'data'=>$data
        ]);
    }

    /**
     * 修改个人信息
     */
    public function edit_personal(Request $request){

        $uid          = session('uid');
        $name         = $request->post('name');
        $value        = $request->post('value');

        if($name=='qq'){
            $data['QQ'] = $value;
        }
        if($name=='wx'){
            $data['wx'] = $value;
        }
        if($name=='alipay'){
            $data['alipay'] = $value;
        }
        if($name=='true_name'){
            $data['true_name'] = $value;
        }
        if($name=='nickname'){
            $data['nickname'] = $value;
        }
        if($name=='bank'){
            $data['bank'] = $value;
        }
        if($name=='branch_bank'){
            $data['branch_bank'] = $value;
        }
        if($name=='bank_card'){
            $data['bank_card'] = $value;
        }
        if($name=='head'){
            $data['head'] = $value;
        }

        $data['update_at'] = time();
        $result = Db::name('member')->where(array('id'=>$uid))->update($data);

        if($result){
            json_return(200,'修改成功');
        }else{
            json_return(500,'修改失败');
        }
    }

    /**
     * 密码
     */
    public function login_password()
    {
        return view();
    }

    /**
     * 修改密码
     */
    public function do_login_password(Request $request){
    $uid        =  session('uid');
    $old_pass   = $request->post('old_pass');
    $new_pass1  = $request->post('new_pass1');
    $new_pass2  = $request->post('new_pass2');
    if(!$old_pass || !$new_pass1 || !$new_pass2 || !$uid ){
    json_return(204,'缺少参数');
    }
    if($new_pass1 != $new_pass2){
        json_return(201,'两次密码不同');
    }
    $my_pass = Db::name('member')->where(array('id'=>$uid))->value('password');

    if(md5($old_pass) != $my_pass){
        json_return(201,'原密码不正确');
    }
    $result = Db::name('member')
        ->where(array('id'=>$uid))
        ->update(array('password'=>md5($new_pass1)));
    if($result){
        json_return(200,'修改密码成功');
    }else{
        json_return(500,'失败');
    }
}

    /**
     * 回水记录
     */
    public function back_record(Request $request){
        $page    = $request->post('page')?$request->post('page'):1;
        $limit = 20;
        $start = ($page-1)*$limit;
        web_check();
        $uid = session('uid');
        $w['d.uid']    = $uid;
        $w['d.status'] = 2;
        $w['d.exp']  = array('in','3,6');
        $limit = 20;
        $data = Db::name('detail')
            ->alias('d')
            ->field('d.money,d.id,d.create_at,c.name as cate,h.name as hall')
            ->join('dl_cate c','d.cate=c.id')
            ->join('dl_hall h','d.hall=h.hall and h.cate=d.cate')
            ->where($w)
            ->order('d.id desc')
            ->limit($start,$limit)
            ->select();
        $total = Db::name('detail')
            ->alias('d')
            ->where($w)
            ->sum('money');
        $return_data['total']      = $total;
        $return_data['data']       = $data;
        $return_data['now_page']   = $page;
        $return_data['per_page']   = $limit;
        $return_data['total_page'] = 10;
        return view('',$return_data);
    }

    /**
     * 回水记录
     */
    public function back_api(Request $request){
        $page    = $request->param('page')?$request->param('page'):1;
        $limit = 20;
        $start = ($page-1)*$limit;
        $uid = session('uid');
        $w['d.uid']    = $uid;
        $w['d.status'] = 2;
        $w['d.exp']  = array('in','3,6');
        $limit = 20;
        $data = Db::name('detail')
            ->alias('d')
            ->field('d.money,d.id,d.create_at,c.name as cate,h.name as hall')
            ->join('dl_cate c','d.cate=c.id')
            ->join('dl_hall h','d.hall=h.hall and h.cate=d.cate')
            ->where($w)
            ->order('d.id desc')
            ->limit($start,$limit)
            ->select();
        foreach($data as $k=>$v){
            $data[$k]['create_at']=date('Y-m-d',$v['create_at']);
        }
        $return_data['data']       = $data;
        $return_data['now_page']   = $page;
        $return_data['per_page']   = $limit;
        $return_data['total_page'] = 10;
       json_return(200,'成功',$return_data);
    }

    /**
     * 额度转化
     */
    public function quota(){
        web_check();
        $uid = session('uid');
        $user_info = Db::name('member')
            ->field('money,gm_name')
            ->where('id',$uid)
            ->find();
        $mobile = $user_info['gm_name'];

        $API = new Biapi();
        $ag_money = $API->balances('AG',$mobile);
        $bb_money = $API->balances('BB',$mobile);
        return view('',[
            'money'=>$user_info['money'],
            'ag_money'=>$ag_money,
            'bb_money'=>$bb_money,
        ]);
    }

    /**
     * 转账到AG、BB
     */
    public function money_to_game(Request $request){
        $uid        = session('uid');
        $money      = $request->post('money');
        $type       = $request->post('type');
        $game       = $request->post('game');
        $money      =floor($money);
        if(!$uid || !$money ||!$type || !$game ){
            json_return(204,'缺少参数');
        }
        if($game=='AG'){
            $cate = 13;
        }elseif ($game=='BB'){
            $cate = 14;
        }else{
            json_return(201,'游戏类型不正确');
        }
        if($type==1) {//转入AG、BB平台
            $user_info = Db::name('member')->where(array('id' => $uid))->field('mobile,money,is_ag,is_bb,gm_name,star')->find();

            $star = $user_info['star'];
            $limit_money = Db::name('star')->where('star',$star)->value('up');
            if($money>$limit_money){
                json_return(201,'转入金额超出限制');
            }

            $is_open = 0;
            if($game=='AG' && $user_info['is_ag']==1){
                $is_open = 1;
            }
            if($game=='BB' && $user_info['is_bb']==1){
                $is_open = 1;
            }
            $my_money = $user_info['money'];
            $mobile   =  $user_info['gm_name'];
            if ($my_money < $money) {
                json_return(207, '余额不足');
            }
            Db::startTrans();
            try {
                $new_money = bcsub(round($my_money, 2), round($money, 2));
                if($is_open==0){
                    if($game=='AG'){
                        $up_data['is_ag'] = 1;
                    }
                    if($game=='BB'){
                        $up_data['is_bb'] = 1;
                    }
                }
                $up_data['money']=$new_money;
                $result0 = Db::name('member')
                    ->where(array('id' => $uid))
                    ->update($up_data);

                error_reporting(0);
                $API = new Biapi();
                $result2 = $API->zzmoney($game, $mobile, 'IN', $money,$is_open);
                $gm_money = $API->balances($game,$mobile);

                $data['uid'] = $uid;
                $data['type'] = 2;
                $data['money'] = $money;
                $data['balance'] = $new_money;
                $data['g_money'] = $gm_money;
                $data['exp'] = 7;
                $data['cate'] = $cate;
                $data['explain'] = $game . '上分';
                $data['create_at'] = time();
                $result1 = Db::name('detail')->insert($data);

            }catch (\Exception $e){

                Db::rollback();
                json_return(500,'转换失败');
            }
            if($result0 && $result1 && $result2){
                $back['uid']   = $uid;
                $back['money'] = $new_money;
                $back['gm_money'] = $gm_money;
                Db::commit();
                json_return(200,'转入成功',$back);
            }
        }
        if($type==2) {//转出AG、BB平台
            $API = new Biapi();
            $user_info = Db::name('member')->where(array('id'=>$uid))->field('gm_name,mobile,money')->find();
            $yc_money  = $user_info['money'];
            $mobile    = $user_info['gm_name'];
            $my_money = $API->balances($game,$mobile);
            if($my_money<$money){
                json_return(207,'余额不足');
            }
            Db::startTrans();
            try {
                $new_money = $yc_money+$money;
                $result0 = Db::name('member')
                    ->where(array('id'=>$uid))
                    ->update(array('money'=>$new_money));
                $data['uid']          = $uid;
                $data['type']         = 1;
                $data['money']        = $money;
                $data['balance']      = $new_money;
                $data['exp']          = 7;
                $data['cate']         = $cate;
                $data['g_money']      = $my_money-$money;
                $data['explain']      = $game.'下分';
                $data['create_at']    = time();
                $result1 = Db::name('detail')->insert($data);
                $result2 = $API-> zzmoney($game,$mobile,'OUT',$money);
            }catch (\Exception $e){
                Db::rollback();
                json_return(500,'转出失败');
            }
            if($result0 && $result1 && $result2){
                Db::commit();
                $back['uid']   = $uid;
                $back['money'] = $new_money;
                $back['gm_money'] = $my_money-$money;
                json_return(200,'转出成功',$back);
            }
        }

    }

    /**
     * 修改头像
     */
    public function upload_head(){
        $uid        = session('uid');
        $file = request()->file('file');
        if(!$file){
            $data = array("status" =>0,"error" => '请选择上传图片');
            echo json_encode($data);
            exit;
        }
        $path = ROOT_PATH . 'public' . DS . 'uploads'. DS .'cate_img';
        $info = $file->move($path);
        if($info){
            $picd = 'cate_img/'.$info->getSaveName();
            $pic = Config::get('img_url').$picd;
            $up = [
                'head'=>$picd
            ];
            Db::name('member')->where('id',$uid)->update($up);
            $data = array("status" =>1,"pic" => $pic,'picd'=>$picd);
            echo json_encode($data);
            exit;
        }else{
            $data = array("status" =>0,"error" => '上传图片失败');
            echo json_encode($data);
            exit;
        }
    }
}